<template>
	<div class="changePassword-container">
		<el-dialog v-model="state.isShowDialog" draggable :close-on-click-modal="false" width="700px" :show-close="false">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-View /> </el-icon>
					<span> {{ $t('message.user_label.changePwd') }}</span>
				</div>
			</template>
			<div style="color: red; padding: 10px 10px; background: #faecd8; margin-bottom: 30px">
				<el-icon style="transform: translateY(2px)"><ele-Bell /></el-icon>
				<span>  {{ $t('message.user_msg_info.accountSecurityPolicy') }} </span>
			</div>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto">
				<el-row :gutter="10">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label= "$t('message.user_label.oldPwd')" prop="passwordOld" :rules="[{ required: true, message: '当前密码不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.passwordOld" type="password" autocomplete="off" show-password />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.user_label.newPwd')" prop="passwordNew" :rules="[{ required: true, message: '新密码不能为空', trigger: 'blur' }]">
							<el-input v-model="state.ruleForm.passwordNew" type="password" autocomplete="off" show-password />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item :label="$t('message.user_label.newPwd2')" prop="passwordNew2" :rules="[{ validator: validatePassword, required: true, trigger: 'blur' }]">
							<el-input v-model="state.passwordNew2" type="password" autocomplete="off" show-password />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<!-- <el-button @click="cancel">取 消</el-button> -->
					<el-button type="primary" @click="submit">{{ $t('message.formI18nBtn.confirm') }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { ElMessageBox } from 'element-plus';
import { sm2 } from 'sm-crypto-v2';

import { clearAccessTokens, getAPI } from '/@/utils/axios-utils';
import { SysUserApi } from '/@/api-services/api';
import { ChangePwdInput } from '/@/api-services/models';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as ChangePwdInput,
	passwordNew2: '',
});

// 打开弹窗
const openDialog = () => {
	state.isShowDialog = true;
	ruleFormRef.value?.resetFields();
};

// // 取消
// const cancel = () => {
// 	state.isShowDialog = false;
// };

// 提交
const submit = () => {
	ruleFormRef.value?.validate(async (valid: boolean) => {
		if (!valid) return;

		// SM2加密密码
		const cpwd: ChangePwdInput = { passwordOld: '', passwordNew: '' };
		const publicKey = window.__env__.VITE_SM_PUBLIC_KEY;
		cpwd.passwordOld = sm2.doEncrypt(state.ruleForm.passwordOld, publicKey, 1);
		cpwd.passwordNew = sm2.doEncrypt(state.ruleForm.passwordNew, publicKey, 1);
		await getAPI(SysUserApi).apiSysUserChangePwdPost(cpwd);

		// 退出系统
		ElMessageBox.confirm(t('message.user_msg_info.loginAgainConfirm'), t('message.formI18nTip.tip'), {
			confirmButtonText: t('message.formI18nBtn.confirm'),
			cancelButtonText: t('message.formI18nBtn.cancel'),
			type: 'warning',
		}).then(async () => {
			state.isShowDialog = false;
			clearAccessTokens();
		});
	});
};

// 密码验证
const validatePassword = (_rule: any, value: any, callback: any) => {
	if (state.passwordNew2 != state.ruleForm.passwordNew) {
		callback(new Error(t('message.user_msg_info.pwdInconsistency')));
	} else {
		callback();
	}
};

// 导出对象
defineExpose({ openDialog });
</script>
